<template>
  <div>
    <config-form :initData="dataForm" :span="8" :options="DETAIL_FORM" />
    <div class="carve-up"></div>
    <config-form :initData="quotaForm" :span="8" :options="CUS_FORM" />
    <div class="carve-up" v-if="flag==='A'"></div>
    <el-form :model="approvalForm" ref="approvalForm" label-width="100px" size="small" :rules="rules">
      <el-form-item label="审批结果:" prop="auditResult" v-if="flag === 'A'">
        <el-select placeholder="请选择审批结果" v-model="approvalForm.auditResult" @change="chooseApproal" clearable>
          <el-option v-for="(item,key) of AUDIT_RESULT" :key="key" :label="item" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item label="审批意见:" prop="auditAdvice" v-if="flag === 'A'">
        <el-input v-model="approvalForm.auditAdvice" rows="5" resize="none" type="textarea" placeholder="请输入审批意见" maxlength="200" show-word-limit clearable></el-input>
      </el-form-item>
      <el-form-item style="margin-top: 10px" label-width="130px" v-if="flag == 'A'">
        <el-button type="primary" @click="close" plain>取消</el-button>
        <el-button type="primary" @click="submit" :disabled="isSubmit">
          <fa-icon class="fa-check" />提交</el-button>
      </el-form-item>
      <el-form-item style="margin-top: 10px;text-align:right;" label-width="130px" v-if="flag == 'Q'">
        <el-button type="primary" @click="close" plain>知道了</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { DETAIL_FORM, CUS_FORM, AUDIT_RESULT } from '../js/contant.js'
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { mapState } from 'vuex'
import {disposePhone, encryptIdNo, encryptName} from '@/utils/util'
export default {
  props: {
    editData: Object,
    flag: String
  },
  data () {
    return {
      DETAIL_FORM,
      CUS_FORM,
      AUDIT_RESULT,
      dataForm: {},
      quotaForm: {},
      approvalForm: {
        auditResult: '',
        auditAdvice: ''
      },
      rules: {
        auditResult: [{ required: true, message: '请选择审批结果', trigger: 'change' }],
        auditAdvice: [{ required: true, message: '请输入审批意见', trigger: 'blur' }]
      },
      isSubmit: true
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    // 获取数据
    getData () {
      api.postQueryByIdLoanData(this.editData.quotaDrawId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          let result = { ...data }
          let statusUpdateTime = new Date(result.statusUpdateTime).getTime()
          let newTime = new Date().getTime()
          let statusStopTime = `${parseInt((newTime - statusUpdateTime) / 60000)}min`
          result.newStatusStopTime = statusStopTime
          result.newProduct = this.dicts['product_type'].childMap[result.productName]
          result.newLoanStatus = this.dicts['loan_status'].childMap[result.loanStatus] ? this.dicts['loan_status'].childMap[result.loanStatus] : result.loanStatus
          result.newFeePayType = this.dicts['fee_pay_type'].childMap[result.feePayType]
          result.newLoanPeriods = this.dicts['loan_periods'].childMap[result.loanPeriods]
          result.newLoanRate = `${result.loanRate}%`
          result.newPhone = disposePhone(result.customerPhone)
          result.newName = encryptName(result.customerName)
          result.newIdNbr = encryptIdNo(result.idNbr)
          this.dataForm = {...result} || {}
          this.queryQuotaById()
        }
      })
    },
    queryQuotaById () {
      api.postQueryByCaseIdLoanData(this.editData.caseId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          let result = { ...data }
          this.quotaForm = Object.assign(this.dataForm, result)
        }
      })
    },
    // 提交
    submit () {
      this.$refs['approvalForm'].validate((valid) => {
        if (valid) {
          let data = { ...this.approvalForm }
          data.quotaDrawId = this.editData.quotaDrawId
          api.postSubmitLoanData(data).then(res => {
            const { responseCode, responseMsg: message } = res
            if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
              this.$message({
                message,
                type: 'success'
              })
              this.$emit('uppdataSuccess')
            }
          })
        }
      })
    },
    close () {
      this.$emit('close', false)
    },
    chooseApproal (val) {
      this.isSubmit = !this.approvalForm.auditResult
    }
  },
  computed: {
    ...mapState({
      products: state => state.common.products,
      dicts: state => state.common.dicts
    })
  }
}
</script>

<style lang="less" scoped>
.carve-up {
  height: 20px;
  margin: 30px 0 10px 0;
  border-top: 1px solid #ddd;
}
</style>
